package code.example.zookeeper.zk;


import code.example.zookeeper.ZkConstant;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.locks.LockSupport;

@RunWith(JUnit4.class)
public class ZookeeperTest {
    private Logger logger = LoggerFactory.getLogger(ZookeeperTest.class);
    ConnectTest connectTest = null;
    StringBuffer sb = new StringBuffer();
    @Before
    public void before(){
        this.connectTest= new ConnectTest(ZkConstant.connectString,sb);

    }
    @After
    public void after(){}


    @Test
    public void testConnect() throws Exception {
        ZooKeeper zooKeeper =    connectTest.getZooKeeper();
        LockSupport.park();
    }

    public void testCreate(String path , byte [] data, CreateMode mode) throws Exception {
       String returnPath =  connectTest.getZooKeeper().create(path,data, ZooDefs.Ids.OPEN_ACL_UNSAFE,mode);
       logger.info("create "+returnPath+" success");
    }


    public void testSetData(String path,byte [] data) throws Exception{
        connectTest.getZooKeeper().setData(path,data,1);
    }
    public void testDelete() throws Exception{
        connectTest.getZooKeeper();
    }

    public void setWatcher(String path) throws Exception{
        testExists(path);
        testGetData(path);
        testGetChildren(path);
    }
    private void testExists(String path) throws Exception{
        Stat stat = connectTest.getZooKeeper().exists(path,true);//使用默认监视器
        logger.info("\"Exists\":{");
        logger.info("\"stat\":\""+logStat(stat)+"\"");
        logger.info("\"watch\":true");
        logger.info("           }");
    }
    private void testGetData(String path) throws Exception{
        Stat stat = new Stat();
        byte [] data = connectTest.getZooKeeper().getData(path,true,stat);
        StringBuilder sb = new StringBuilder();
        sb.append("\"getData\":{");
        sb.append("\"path\":\""+path+"\"");
        sb.append("\"data\"=\""+new String (data,0,data.length)+"\"");
        sb.append("\"stat\":\""+logStat(stat)+"\"");
        sb.append("\"watch\":true");
        sb.append("           }");
        logger.info(sb.toString());
    }

    private void testGetChildren(String path) throws Exception{

        List<String> children = connectTest.getZooKeeper().getChildren(path,true);
        logger.info("\"getChildren\":{");
        logger.info("\"children\"="+ logChildern(children));
        logger.info("\"watch\":true");
        logger.info("           }");
    }


    public static void main(String args []){

    }

    private String logChildern(List<String> child){
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for(String str:child){
            sb.append(str).append(",");
        }
        if(sb.length()>2){
            sb.deleteCharAt(sb.length()-1);
        }
        sb.append("]");
        return sb.toString();

    }
    private String  logStat(Stat stat){
        StringBuilder sb = new StringBuilder();
        sb.append("stat.").append("ctime:").append(stat.getCtime()).append(",");
        sb.append("stat.").append("czxid:").append(stat.getCzxid()).append(",");
        sb.append("stat.").append("mtime:").append(stat.getMtime()).append(",");
        sb.append("stat.").append("mzxid:").append(stat.getMzxid()).append(",");
        sb.append("stat.").append("pzxid:").append(stat.getPzxid()).append(",");
        sb.append("stat.").append("version:").append(stat.getVersion()).append(",");
        sb.append("stat.").append("aversion:").append(stat.getAversion()).append(",");
        sb.append("stat.").append("cversion:").append(stat.getCversion()).append(",");
        sb.append("stat.").append("ephemeralOwner:").append(stat.getEphemeralOwner()).append(",");
        sb.append("stat.").append("numChildren:").append(stat.getNumChildren()).append(",");
        sb.append("stat.").append("dataLength:").append(stat.getDataLength()).append(",");
        return sb.toString();
    }
}
